<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Adv;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class AdvController extends Controller
{
    protected $keys = [
        'cache:adv'
    ];

    protected function clear()
    {
        foreach ($this->keys as $key) {
            Cache::forget($key);
        }
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Adv $adv)
    {
        $items = $adv->with('nav')->get();
        return $this->successful($items);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Adv::create($request->all());
        $this->clear();
        return $this->successful();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Adv $adv)
    {
        return $this->successful($adv);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Adv $adv)
    {
        $adv->update($request->all());
        $this->clear();

        return $this->successful($adv);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Adv $adv)
    {
        $adv->delete();
        return $this->successful([], '删除成功');
    }

    public function batchDelete()
    {
        $ids = request()->post('id');
        if ($ids) {
            Adv::destroy($ids);
        }
        return $this->successful([], '删除成功');
    }
}
